package com.l.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.l.entity.Upload;
import com.l.service.UploadService;
import com.l.utils.FileEntity;
import com.l.utils.FileUpload;
import com.l.utils.FileUploadTool;
import com.l.utils.Msg;

//上传图片，视频，音频(只要修改一下设置的上传大小，则图片，视频，音频，文档可以使用同一个方法)
@RestController
public class UploadController {
	@Autowired
	UploadService uploadService;
	
	//上传图片
	@RequestMapping("/insertUploadtupian")
	public Msg insertUploadtupian(HttpServletRequest request,MultipartFile file) throws Exception{
		String filename=FileUpload.uploadFile(file, request);
		Upload upload=new Upload();
		upload.setPhoto(filename);
		uploadService.insertUpload(upload);
		return Msg.success();
	}
	//上传视频
	@RequestMapping("/insertUploadshipin")
	public Msg insertUploadshipin(HttpServletRequest request,MultipartFile file) throws Exception{
        FileEntity entity = new FileEntity();
        FileUploadTool fileUploadTool = new FileUploadTool();
        try {
            entity = fileUploadTool.createFile(file, request);
            if (entity != null) {
            	Upload upload=new Upload();
        		upload.setPhoto(entity.getPath());
        		uploadService.insertUpload(upload);
        		System.out.println(entity);
            	return Msg.success().add("path", entity.getPath());
                
            } else {
            	return Msg.fail();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Msg.success();
	}
	//上传音频
	@RequestMapping("/insertUploadyinpin")
	public Msg insertUploadyinpin(HttpServletRequest request,MultipartFile file) throws Exception{
		String filename=FileUpload.uploadFile(file, request);
		Upload upload=new Upload();
		upload.setPhoto(filename);
		uploadService.insertUpload(upload);
		return Msg.success();
	}
}
